<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Working with strings</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Qt4 utility classes, tutorial, C++, programming, GUI, multiplatform, time, date">
<meta name="description" content="In this part of the Qt4 tutorial, we will work with strings.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5536206-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Working with strings</h1>


<p>
In this chapter we will work with strings. Qt4 has a QString class for working 
with strings. It is very powerful and has numerous methods. 
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
The QString class provides a Unicode character string. It stores a string as 16-bit 
QChars. Each QChar corresponds to one Unicode 4.0 character. 
Unlike strings in many other programming languages, a QString can be modified.
</p>


<h2>First example</h2>

<p>
In the first example, we will work with a few basic methods of the QString
class. 
</p>

<div class="codehead">basic.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main(void)
{
   QTextStream out(stdout);
   
   QString a = "love";
   
   a.append(" chess");
   a.prepend("I ");
   
   out &lt;&lt; a &lt;&lt; endl;
   out &lt;&lt; "The a string has " &lt;&lt; a.count() 
       &lt;&lt; " characters" &lt;&lt; endl;
       
   out &lt;&lt; a.toUpper() &lt;&lt; endl;    
   out &lt;&lt; a.toLower() &lt;&lt; endl;
   
   return 0;
}
</pre>

<p>
In the code example, we initiate a QString. We append and prepend some 
additional text. We print the length of the string. Finally, we print the
modified string in upper and lower case.
</p>

<pre class="explanation">
QString a = "love";
</pre>

<p>
A <code>QString</code> is initiated. 
</p>

<pre class="explanation">
a.append(" chess");
a.prepend("I ");
</pre>

<p>
We append and prepend text to the initial string. The string is modified
in-place. 
</p>


<pre class="explanation">
out &lt;&lt; a &lt;&lt; endl;
</pre>

<p>
'I love chess' is printed to the terminal.
</p>

<pre class="explanation">
out &lt;&lt; "The a string has " &lt;&lt; a.count() 
    &lt;&lt; " characters" &lt;&lt; endl;
</pre>

<p>
The <code>count()</code> method returns the number of characters 
in the string. The <code>length()</code> and <code>size()</code>
methods are equivalents. 
</p>

<pre class="explanation">
out &lt;&lt; a.toUpper() &lt;&lt; endl;    
out &lt;&lt; a.toLower() &lt;&lt; endl;
</pre>

<p>
These two methods return an uppercase and lowercase copy of the string.
They do not modify the string, they return a new modified copy of the
string.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./basic 
I love chess
The a string has 12 characters
I LOVE CHESS
i love chess
</pre>


<h2>Initiating strings</h2>

<p>
A QString can be initiated in several ways. 
</p>

<div class="codehead">init.cpp</div>
<pre class="code">
#include &lt;QTextStream>

int main(void)
{
   QTextStream out(stdout);
   
   QString str1 = "The night train";
   out &lt;&lt; str1 &lt;&lt; endl;
   
   QString str2("A yellow rose");
   out &lt;&lt; str2 &lt;&lt; endl;
   
   std::string s1 = "A blue sky";
   QString str3 = s1.c_str(); 
   out &lt;&lt; str3 &lt;&lt; endl;
   
   std::string s2 = "A thick fog";
   QString str4 = QString::fromAscii(s2.data(), s2.size());
   out &lt;&lt; str4 &lt;&lt; endl;
   
   char s3[] = "A deep forest";
   QString str5(s3);
   out &lt;&lt; str5 &lt;&lt; endl;
   
   return 0;
}
</pre>

<p>
We present five ways of initiating a QString.
</p>

<pre class="explanation">
QString str1 = "The night train";
</pre>

<p>
This is a traditional way of initiating a string in computer
languages.
</p>

<pre class="explanation">
QString str2("A yellow rose");
</pre>

<p>
This is an object way of initiating a QString.
</p>

<pre class="explanation">
std::string s1 = "A blue sky";
QString str3 = s1.c_str(); 
</pre>

<p>
We have a string object from the C++ standard library. We 
use its <code>c_str()</code> method to generate a null-terminated sequence 
of characters. This array of characters, a classic C representation of a string, 
can be assigned to a QString variable.
</p>

<pre class="explanation">
std::string s2 = "A thick fog";
QString str4 = QString::fromAscii(s2.data(), s2.size());
</pre>

<p>
In these code lines we convert a standard C++ string to a QString.
We utilize the <code>fromAscii()</code> method. It takes a pointer to the
an array of characters. The pointer is returned with the <code>data()</code>
method of the std::string. The second parameter is the the size of the
std::string.
</p>

<pre class="explanation">
char s3[] = "A deep forest";
QString str5(s3);
</pre>

<p>
This is a C string. It is an array of chars. One of the QString constructors can
take an array of chars as a parameter.
</p>


<div class="codehead">Output</div>
<pre class="code">
$ ./init 
The night train
A yellow rose
A blue sky
A thick fog
A deep forest
</pre>


<h2>Accessing string elements</h2>

<p>
A QString is a sequence of QChars. The elements of a string
can be accessed using the [] operator or the <code>at()</code>
method.
</p>

<div class="codehead">access.cpp</div>
<pre class="code">
#include &lt;QTextStream>

int main(void)
{
   QTextStream out(stdout);
   
   QString a = "Eagle";

   out &lt;&lt; a[0] &lt;&lt; endl;
   out &lt;&lt; a[4] &lt;&lt; endl;
   
   out &lt;&lt; a.at(0) &lt;&lt; endl;
   
   if (a.at(5).isNull()) {
     out &lt;&lt; "Outside the range of the string" &lt;&lt; endl;  
   }      
   
   return 0;
}
</pre>

<p>
We print some individual characters from a specific QString.
</p>

<pre class="explanation">
out &lt;&lt; a[0] &lt;&lt; endl;
out &lt;&lt; a[4] &lt;&lt; endl;
</pre>

<p>
We print the first and the fifth element of a string.
</p>

<pre class="explanation">
out &lt;&lt; a.at(0) &lt;&lt; endl;
</pre>

<p>
With the <code>at()</code> method, we retrieve the first
character of the string.
</p>

<pre class="explanation">
if (a.at(5).isNull()) {
    out &lt;&lt; "Outside the range of the string" &lt;&lt; endl;  
}      
</pre>

<p>
The <code>at()</code> method returns null, if we are trying to access
a character outside the range of string characters.
</p>


<div class="codehead">Output</div>
<pre class="code">
$ ./access 
E
e
E
Outside the range of the string
</pre>


<h2>The string length</h2>

<p>
There are three methods to get the length of a string.
The <code>size()</code>, the <code>count()</code> and the
<code>length()</code> method. All do the same thing. They return the
number of characters in the specified string.
</p>

<div class="codehead">length.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
  QTextStream out(stdout);
   
  QString s1 = "Eagle";
  QString s2 = "Eagle\n";
  QString s3 = "Eagle ";
  QString s4 = "орел";

  out &lt;&lt; s1.length() &lt;&lt; endl;
  out &lt;&lt; s2.length() &lt;&lt; endl;
  out &lt;&lt; s3.length() &lt;&lt; endl;
  out &lt;&lt; s4.length() &lt;&lt; endl;
  
  return 0;
}
</pre>

<p>
We get the size of four strings.
</p>

<pre class="explanation">
QString s2 = "Eagle\n";
QString s3 = "Eagle ";
</pre>

<p>
Each of these two strings has a white character.
</p>

<pre class="explanation">
QString s4 = "орел";
</pre>

<p>
This string consists of russian letters.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./length
5
6
6
8
</pre>

<p>
From the output we can see, that the <code>length()</code> method counts
the white characters too. The last string contains unicode letters, where each
letter is stored as two characters.
</p>


<h2>String interpolation</h2>

<p>
String interpolation is a dynamic building of strings. It allows us to 
replace specific control characters with actual values. We use the <code>arg()</code>
method to do the interpolation.
</p>

<div class="codehead">interpolation.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main()
{
   QTextStream out(stdout);
   
   QString s1 = "There are %1 white roses";
   int n = 12;
   
   out &lt;&lt; s1.arg(n) &lt;&lt; endl;
   
   QString s2 = "The tree is %1m high";
   double h = 5.65;
   
   out &lt;&lt; s2.arg(h) &lt;&lt; endl;
   
   QString s3 = "We have %1 lemons and %2 oranges";
   int ln = 12;
   int on = 4;
   
   out &lt;&lt; s3.arg(ln).arg(on) &lt;&lt; endl;
   
   return 0;
}
</pre>

<p>
The markers which are going to be replaced begin with the % character.
The following character is a number specifying the argument. There 
can be multiple of arguments for a string. The <code>arg()</code> method
is overloaded, it can take integers, long numbers, chars, QChars among others.
</p>

<pre class="explanation">
QString s1 = "There are %1 white roses";
int n = 12;
</pre>

<p>
The %1 is the marker which we plan to replace. We have defined one integer.
</p>

<pre class="explanation">
out &lt;&lt; s1.arg(n) &lt;&lt; endl;
</pre>

<p>
The <code>arg()</code> method takes an integer. The %1 marker is replaced with
the value of the n variable.
</p>

<pre class="explanation">
QString s2 = "The tree is %1m high";
double h = 5.65;

out &lt;&lt; s2.arg(h) &lt;&lt; endl;
</pre>

<p>
These three lines do the same thing for a double number. The correct
<code>arg()</code> method is called automatically.
</p>

<pre class="explanation">
QString s3 = "We have %1 lemons and %2 oranges";
int ln = 12;
int on = 4;

out &lt;&lt; s3.arg(ln).arg(on) &lt;&lt; endl;
</pre>

<p>
We can have multiple control characters. The %1 refers to the first
argument, the %2 to the second. The <code>arg()</code> methods are called
in a consecutive chain.
</p>


<div class="codehead">Output</div>
<pre class="code">
$ ./interpolation 
There are 12 white roses
The tree is 5.65m high
We have 12 lemons and 4 oranges
</pre>


<h2>Substrings</h2>

<p>
When doing text processing, we need to find substrings of normal strings.
We have <code>left()</code>, <code>mid()</code> and <code>right()</code> methods
at our disposal.
</p>

<div class="codehead">substrings.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main(void)
{
   QTextStream out(stdout);
   
   QString str = "The night train";
   
   out &lt;&lt; str.right(5) &lt;&lt; endl;
   out &lt;&lt; str.left(9) &lt;&lt; endl;
   out &lt;&lt; str.mid(4, 5) &lt;&lt; endl;
   
   QString str2("The big apple");
   QStringRef sub(&amp;str2, 0, 7);
   
   out &lt;&lt; sub.toString() &lt;&lt; endl;
   
   return 0;
}
</pre>

<p>
We will use all three methods to find some substrings of a given string.
</p>

<pre class="explanation">
out &lt;&lt; str.right(5) &lt;&lt; endl;
</pre>

<p>
With the <code>right()</code> method, we get the 5 rightmost characters 
of the str string. The 'train' is printed.
</p>

<pre class="explanation">
out &lt;&lt; str.left(9) &lt;&lt; endl;
</pre>

<p>
With the <code>left()</code> method, we get the 9 leftmost characters 
of the str string. The 'The night' is printed.
</p>

<pre class="explanation">
out &lt;&lt; str.mid(4, 5) &lt;&lt; endl;
</pre>

<p>
With the <code>mid()</code> method, we get 5 characters starting from 
position 4. The 'night' is printed.
</p>

<pre class="explanation">
QString str2("The big apple");
QStringRef sub(&amp;str2, 0, 7);
</pre>

<p>
The <code>QStringRef</code> class is a read-only version of a QString. 
Here we create a <code>QStringRef</code> of a portion of the str2 string. 
The second parameter is the position and the third is the length of the 
substring.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./substrings 
train
The night
night
The big
</pre>


<h2>Looping through strings</h2>

<p>
A QString consists of QChars. We can loop through the QString to 
access each element of a string.
</p>


<div class="codehead">looping.cpp</div>
<pre class="code">
#include &lt;QTextStream>

int main(void)
{
  QTextStream out(stdout);
   
  QString str = "There are many stars.";
  
  foreach (QChar qc, str) {
    out &lt;&lt; qc &lt;&lt; " ";  
  }
  
  out &lt;&lt; endl;
  
  for (QChar *it=str.begin(); it!=str.end(); ++it) {
    out &lt;&lt; *it &lt;&lt; " " ;
  }
  
  out &lt;&lt; endl;
  
  for (int i = 0; i &lt; str.size(); ++i) {
    out &lt;&lt; str.at(i) &lt;&lt; " ";    
  }
  
  out &lt;&lt; endl;
  
  return 0;
}
</pre>

<p>
We show three ways to go through a QString. We add a space character between the
letters as we print them to the terminal. 
</p>


<pre class="explanation">
foreach (QChar qc, str) {
  out &lt;&lt; qc &lt;&lt; " ";  
}
</pre>

<p>
The <code>foreach</code> keyword is a Qt extension to the C++ language.
The first parameter of the keyword is the string element, the second one
is the string.
</p>

<pre class="explanation">
for (QChar *it=str.begin(); it!=str.end(); ++it) {
  out &lt;&lt; *it &lt;&lt; " " ;
}
</pre>

<p>
In this code, we use iterators to go through the string.
</p>

<pre class="explanation">
for (int i = 0; i &lt; str.size(); ++i) {
  out &lt;&lt; str.at(i) &lt;&lt; " ";    
}
</pre>

<p>
We compute the size of the string and use the <code>at()</code> method to access the 
string elements. 
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./looping
T h e r e   a r e   m a n y   s t a r s . 
T h e r e   a r e   m a n y   s t a r s . 
T h e r e   a r e   m a n y   s t a r s . 
</pre>


<h2>String comparison</h2>

<p>
The <code>QString::compare()</code> static method is used to compare two strings. 
The method returns an integer. If the returned value is less than zero, the first 
string is less than the second. If it returns zero, both strings are equal. Finally, 
if the returned value is greater than zero, the first string is greater than the 
second. By 'less' we mean that a specific character of a string is positioned before 
the other one in the character table. Strings are compared the following way.
The first characters of the two strings are compared. If they are equal, the following
two characters are compared. Until we find some characters that differ or we find
that all characters match. 
</p>

<div class="codehead">comparing.cpp</div>
<pre class="code">
#include &lt;QTextStream>

#define STR_EQUAL 0

int main(void)
{
   QTextStream out(stdout);
   
   QString a = "Rain";
   QString b = "rain";
   QString c = "rain\n";
   
   if (QString::compare(a, b) == STR_EQUAL) {
     out &lt;&lt; "a, b are equal" &lt;&lt; endl;
   } else {
     out &lt;&lt; "a, b are not equal" &lt;&lt; endl;
   }
   
   out &lt;&lt; "In case insensitive comparison:" &lt;&lt; endl;
   
   if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) {
     out &lt;&lt; "a, b are equal" &lt;&lt; endl;
   } else {
     out &lt;&lt; "a, b are not equal" &lt;&lt; endl;
   }     
   
   if (QString::compare(b, c) == STR_EQUAL) {
     out &lt;&lt; "b, c are equal" &lt;&lt; endl;
   } else {
     out &lt;&lt; "b, c are not equal" &lt;&lt; endl;
   }   

   c.chop(1);
   
   out &lt;&lt; "After removing the new line character" &lt;&lt; endl;
   
   if (QString::compare(b, c) == STR_EQUAL) {
     out &lt;&lt; "b, c are equal" &lt;&lt; endl;
   } else {
     out &lt;&lt; "b, c are not equal" &lt;&lt; endl;
   }            
   
   return 0;
}
</pre>

<p>
We will do case sensitive and case insensitive comparison with the 
<code>compare()</code> method.
</p>

<pre class="explanation">
#define STR_EQUAL 0
</pre>

<p>
For better code clarity, we define the STR_EQUAL constant. 
</p>

<pre class="explanation">
QString a = "Rain";
QString b = "rain";
QString c = "rain\n";
</pre>

<p>
We will be comparing these three strings.
</p>

<pre class="explanation">
if (QString::compare(a, b) == STR_EQUAL) {
    out &lt;&lt; "a, b are equal" &lt;&lt; endl;
} else {
    out &lt;&lt; "a, b are not equal" &lt;&lt; endl;
}
</pre>

<p>
We compare the a and b strings. The strings are not equal.
They differ in the first character.
</p>

<pre class="explanation">
if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) {
    out &lt;&lt; "a, b are equal" &lt;&lt; endl;
} else {
    out &lt;&lt; "a, b are not equal" &lt;&lt; endl;
}     
</pre>

<p>
In case of case insensitive comparison, the strings are equal.
The <code>Qt::CaseInsensitive</code> makes the comparison case insensitive.
</p>

<pre class="explanation">
c.chop(1);
</pre>

<p>
The <code>chop()</code> method removes the last character from the c string.
Now the b, c strings are equal.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./comparing 
a, b are not equal
In case insensitive comparison:
a, b are equal
b, c are not equal
After removing the new line character
b, c are equal
</pre>


<h2>Converting strings</h2>

<p>
Strings often need to be converted to other data types. And vice versa.
The <code>toInt()</code>, <code>toFloat()</code>, <code>toLong()</code> 
are three QString methods which convert a string to an integer, float and
long number. (There are more such methods.) The <code>setNum()</code> method 
converts various numeric data types to a string. The method is overloaded 
and the correct one is called automatically. 
</p>

<div class="codehead">Output</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main(void)
{
  QTextStream out(stdout);
   
  QString s1 = "12";
  QString s2 = "15";
  QString s3, s4;
  
  out &lt;&lt; s1.toInt() + s2.toInt() &lt;&lt; endl;
   
  int n1 = 30;
  int n2 = 40;
  
  out &lt;&lt; s3.setNum(n1) + s4.setNum(n2) &lt;&lt; endl;
   
  return 0;
}
</pre>

<p>
In the example we convert two strings to integers and add them. Then
we convert two integers to strings and concatenate them.
</p>


<pre class="explanation">
out &lt;&lt; s1.toInt() + s2.toInt() &lt;&lt; endl;
</pre>

<p>
The <code>toInt()</code> method converts a string to an integer. We add
two numbers converted froms strings.
</p>


<pre class="explanation">
out &lt;&lt; s3.setNum(n1) + s4.setNum(n2) &lt;&lt; endl; 
</pre>

<p>
In this case the <code>setNum()</code> method converts an integer to 
a string. We concatenate two strings.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./converts 
27
3040
</pre>


<h2>Letters</h2>

<p>
Characters are divided into various categories. Digits, letters, spaces or
punctuation characters. Each QString consists of QChars. The QChar has
the <code>isDigit()</code>, <code>isLetter()</code>, <code>isSpace()</code> and
<code>isPunct()</code> method to perform the job. 
</p>

<div class="codehead">letters.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main(void)
{
  QTextStream out(stdout);
   
  int digits  = 0;
  int letters = 0;
  int spaces  = 0;
  int puncts  = 0;
  
  QString str = "7 white, 3 red roses.";
  
  foreach(QChar s, str)
  {
    if (s.isDigit()) {
      digits++;
    } else if (s.isLetter()) {
      letters++;
    } else if (s.isSpace()) {
      spaces++;
    } else if (s.isPunct()) {
      puncts++;
    }    
  }  
  
  out &lt;&lt; QString("There are %1 characters").arg(str.count()) &lt;&lt; endl;
  out &lt;&lt; QString("There are %1 letters").arg(letters) &lt;&lt; endl;
  out &lt;&lt; QString("There are %1 digits").arg(digits) &lt;&lt; endl;
  out &lt;&lt; QString("There are %1 spaces").arg(spaces) &lt;&lt; endl;
  out &lt;&lt; QString("There are %1 punctuation characters").arg(puncts) &lt;&lt; endl;
    
  return 0;
}
</pre>

<p>
In the example we define a simple sentence. We will count the number of digits, letters,
spaces and punctuation characters in the sentence. 
</p>

<pre class="explanation">
int digits  = 0;
int letters = 0;
int spaces  = 0;
int puncts  = 0;
</pre>

<p>
We define an integer variable for each character category.
</p>

<pre class="explanation">
QString str = "7 white, 3 red roses.";
</pre>

<p>
This is the sentence to be examined. 
</p>

<pre class="explanation">
foreach(QChar s, str)
{
  if (s.isDigit()) {
    digits++;
} else if (s.isLetter()) {
    letters++;
} else if (s.isSpace()) {
    spaces++;
} else if (s.isPunct()) {
    puncts++;
  }    
}  
</pre>

<p>
We use the foreach keyword to go through the QString. Each of the elements
is a QChar. We use the methods of the QChar class to determine the categories
of characters. 
</p>

<pre class="explanation">
out &lt;&lt; QString("There are %1 characters").arg(str.count()) &lt;&lt; endl;
out &lt;&lt; QString("There are %1 letters").arg(letters) &lt;&lt; endl;
out &lt;&lt; QString("There are %1 digits").arg(digits) &lt;&lt; endl;
out &lt;&lt; QString("There are %1 spaces").arg(spaces) &lt;&lt; endl;
out &lt;&lt; QString("There are %1 punctuation characters").arg(puncts) &lt;&lt; endl;
</pre>

<p>
Using the string interpolation, we print the numbers to the terminal. 
</p>


<div class="codehead">Output</div>
<pre class="code">
$ ./letters 
There are 21 characters
There are 13 letters
There are 2 digits
There are 4 spaces
There are 2 punctuation characters
</pre>


<h2>Modifying strings</h2>

<p>
Some methods (for example toLower() method) return a new modified copy
of an original string. Other methods modify the string in-place. 
We will present some of them.  
</p>

<div class="codehead">modify.cpp</div>
<pre class="code">
#include &lt;QTextStream&gt;

int main(void)
{
   QTextStream out(stdout);
   
   QString str = "Lovely";   
   str.append(" season");
   
   out &lt;&lt; str &lt;&lt; endl;
   
   str.remove(10, 3);
   out &lt;&lt; str &lt;&lt; endl;

   str.replace(7, 3, "girl");
   out &lt;&lt; str &lt;&lt; endl;
   
   str.clear();
   
   if (str.isEmpty()) {
     out &lt;&lt; "The string is empty" &lt;&lt; endl;
   }
   
   return 0;
}
</pre>

<p>
We describe four methods that modify a string in-place.
</p>

<pre class="explanation">
str.append(" season");
</pre>

<p>
The <code>append()</code> method adds a new string at the end
of the string.
</p>

<pre class="explanation">
str.remove(10, 3);
</pre>

<p>
The <code>remove()</code> method removes 3 characters from the string, 
starting from position 10.
</p>

<pre class="explanation">
str.replace(7, 3, "girl");
</pre>

<p>
The <code>replace()</code> method replaces 3 characters beginning at position 7 
with the specified string. 
</p>

<pre class="explanation">
str.clear();
</pre>

<p>
The <code>clear()</code> method clears the string.
</p>

<div class="codehead">Output</div>
<pre class="code">
$ ./modify 
Lovely season
Lovely sea
Lovely girl
The string is empty
</pre>


<p>
In this chapter, we have worked with strings in Qt4.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified October 23, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

